/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.hargaitembiaya;

import java.util.Map;
import org.sadhar.sipp.posbiaya.*;
import org.sadhar.sia.framework.ClassApplicationModule;
import org.sadhar.sipp.perekamanitembiaya.PerekamanItemBiayaDAO;
import org.sadhar.sipp.perekamanitembiaya.PerekamanItemBiayaDAOImpl;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author bloodrunk
 */
public class HargaItemPosBiayaPopupWnd extends ClassApplicationModule {

    private Listbox lstboxPosBiaya;
    private Textbox txtboxSearch;
    private Window window;
    private HargaItemBiayaDAO hargaItemBiayaDAO;
    private PerekamanItemBiayaDAO perekamanItemBiayaDAO;
    private Label lblTahunAnggaran;
    private Listbox lstboxItemBiaya;
    private int indexNo = 1;

    public HargaItemPosBiayaPopupWnd() {
        hargaItemBiayaDAO = new HargaItemBiayaDAOImpl();
        perekamanItemBiayaDAO = new PerekamanItemBiayaDAOImpl();
    }

    public void onCreate() throws Exception {
        window = (Window) this.getRoot().getFellow("contentDiv").getFirstChild();
        lstboxPosBiaya = (Listbox) getFellow("lstboxPosBiaya");
        txtboxSearch = (Textbox) getFellow("txtboxSearch");
        lblTahunAnggaran = (Label) window.getFellow("lblTahunAnggaran");
        lstboxItemBiaya = (Listbox) window.getFellow("lstboxItemBiaya");
        load();
    }

    public void load() throws Exception {
        lstboxPosBiaya.getItems().clear();
        int no = 1;
        for (PosBiaya posBiaya : hargaItemBiayaDAO.getPosBiaya(txtboxSearch.getValue(), lblTahunAnggaran.getValue())) {
            Listitem item = new Listitem();
            item.setValue(posBiaya);
            item.appendChild(new Listcell("" + no++));
            item.appendChild(new Listcell(posBiaya.getNamaPosBiaya()));
            lstboxPosBiaya.appendChild(item);
        }
    }

    public void lstboxPosBiayaOnSelect() throws Exception {
        Textbox txtboxKeySearch = (Textbox) window.getFellow("txtboxKeySearch");
        Textbox txtboxIdPosBiaya = (Textbox) window.getFellow("txtboxIdPosBiaya");
        Label lblNamaPosBiaya = (Label) window.getFellow("lblNamaPosBiaya");
        Button btnSimpan = (Button) window.getFellow("btnSimpan");
        lblTahunAnggaran = (Label) window.getFellow("lblTahunAnggaran");
        PosBiaya posBiaya = (PosBiaya) lstboxPosBiaya.getSelectedItem().getValue();
        txtboxKeySearch.setValue(posBiaya.getNamaPosBiaya());
        txtboxIdPosBiaya.setValue(posBiaya.getKodePosBiaya() + "");
        lblNamaPosBiaya.setValue(posBiaya.getNamaPosBiaya());
        loadHargaItemBiaya(txtboxIdPosBiaya, window);
        this.detach();
    }

    public void loadHargaItemBiaya(Textbox textbox, Window window) throws Exception {
      /*  lstboxItemBiaya.getItems().clear();
        int indexHarga = 0;
        int no = 1;
        for (final Map itemBiaya : perekamanItemBiayaDAO.select(Integer.valueOf(textbox.getValue()))) {
            Listcell listcell = new Listcell();
            Listitem item = new Listitem();
            item.setValue(itemBiaya);
            item.appendChild(new Listcell("" + no++));
            item.appendChild(new Listcell(itemBiaya.get("namaItem").toString()));

            Textbox txtboxHarga = new Textbox();
            txtboxHarga.setId("txtboxHarga" + indexHarga++);
            listcell.appendChild(txtboxHarga);
            item.appendChild(listcell);
            lstboxItemBiaya.appendChild(item);
        }*/

        lstboxItemBiaya.getItems().clear();
        int indexHarga = 0;
        int no = 1;
        for (final Map hargaItemBiaya : hargaItemBiayaDAO.listByPosBiaya(Integer.valueOf(textbox.getValue()),lblTahunAnggaran.getValue())) {
             Listcell listcell = new Listcell();
             Listitem item2 = new Listitem();
             item2.setValue(hargaItemBiaya);
             item2.appendChild(new Listcell("" + no++));
             item2.appendChild(new Listcell(hargaItemBiaya.get("namaItem").toString()));

             Textbox txtboxHarga = new Textbox();
             txtboxHarga.setId("txtboxHarga" + indexHarga++);
             txtboxHarga.setValue(hargaItemBiaya.get("harga").toString());
             txtboxHarga.setDisabled(false);
             System.out.println(txtboxHarga.getId());

             listcell.appendChild(txtboxHarga);
             item2.appendChild(listcell);
             lstboxItemBiaya.appendChild(item2);
        }


    }
}
